package com.robot.topology.tools;

import java.util.ArrayList;

public class StrategyTools {

//CONVENTIONAL REWARD/PUNISHMENT	
	//Method to update the edges
	//Reward is a float
	public static Double linearBM (Double weight, Double ratio, Double reward){
		//f ij (k+1) =a f ij (k) + (1-a) b(k);
		return (
				(ratio.doubleValue()*weight.doubleValue())
				+
				(1-ratio.doubleValue())
				*reward.doubleValue());
	}

	public static Double incremental (Double weight, Double ratio, Double reward){
		//The incremental learning algorithm.
		//f ij (k+1) = f ij (k) + tb(k) ;
		return (
				weight.doubleValue()
				+ (ratio.doubleValue()*reward.doubleValue())
				);
	}

//INVERSELY PROPORTIONAL REWARD/PUNISHMENT 	
	public static Double invProportionalBM (Double weight, Double ratio, Double reward, Integer pathLength){
		//f ij (k+1) =a f ij (k) + (1-a) *100/pathlength;
		return (
				(ratio.doubleValue()*weight.doubleValue())
				+
				(
						1-ratio.doubleValue()
					/
						(pathLength.intValue())
				)
				);
	}

	public static Double invProportionalInc (Double weight, Double ratio, Double reward, Integer pathLength){
		//The incremental learning algorithm.
		//f ij (k+1) = f ij (k) + t*100/pathlength ;
		return (
				weight.doubleValue()
				+ (ratio.doubleValue()/pathLength.intValue()
						)
				);
	}

	
	public static ArrayList<Double> getProbabilities(ArrayList<Double> listWeights){
		ArrayList<Double> listToReturn = new ArrayList<Double>();
		Double totalWeight=(double)0;
		for(Double currentWeight : listWeights)
			totalWeight+=currentWeight;

		for(int i=0; i<listWeights.size(); i++)
			listToReturn.add(i, listWeights.get(i));

		return listToReturn;

	}
	
	/**
	 * Update de pheromone with delta reinforce
	 * @author djurado
	 */
	public static Double ACO_Reinforce(Double weight, Integer nArcs, Double delta){
		//f ij (k+1) = f ij (k) + δ /L(k)
		Double w = weight + (delta/nArcs); 
		return (w > 1.0 ? 1.0 : w);
	}
	
	/**
	 * Apply the (alpha) decay of pheromone
	 * @author djurado
	 */
	public static Double ACO_Decay(Double weight, Double alpha){
		//f ij (k+1) = α xf ij (k) = f ij (k) x (1 - α) 
		return (weight * (1 - alpha) );
	}
	
}
